package com.dhcc.DRGsHosp.facade.impl;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.QueryChannelService;
import org.dayatang.utils.Page;
import org.openkoala.koala.commons.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;

import com.dhcc.DRGsHosp.application.CostSubApplication;
import com.dhcc.DRGsHosp.core.domain.CostSub;
import com.dhcc.DRGsHosp.facade.CostSubFacade;
import com.dhcc.DRGsHosp.facade.dto.CostSubDTO;
import com.dhcc.DRGsHosp.facade.impl.assembler.CostSubAssembler;

@Named("drgsHospFacadeImplchl20180314")
public class CostSubFacadeImpl implements CostSubFacade {

	@Autowired
	private CostSubApplication  application;

	private QueryChannelService queryChannel;

    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
	
	public void creatCostSub(CostSubDTO costSubDTO) {
		application.creatCostSub(CostSubAssembler.toEntity(costSubDTO));
	}
	
	public InvokeResult removeCostSub(String costSubCode) {
		application.removeCostSub(application.getCostSub(costSubCode));
		return InvokeResult.success();
	}
	
	public InvokeResult removeCostSubs(String[] costSubCodes) {
		for(String costSubCode:costSubCodes) {
			CostSub cs = new CostSub() ;
			cs.setCostSubcode(costSubCode);
			application.removeCostSub(cs);
		}
		return InvokeResult.success();
	}
	
	public List<CostSubDTO> findAllCostSub() {
		return CostSubAssembler.toDTOs(application.findAllCostSub());
	}
	
	public Page<CostSubDTO> pageQueryCostSub(CostSubDTO costCubDto, int currentPage, int pageSize) {
		List<Object> conditionVals = new ArrayList<Object>();
	   	StringBuilder jpql = new StringBuilder("select _costSub from CostSub _costSub   where 1=1 ");
	   	if (costCubDto.getCostSubdesc() != null && !"".equals(costCubDto.getCostSubdesc())) {
	   		jpql.append(" and _costSub.costSubdesc like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", costCubDto.getCostSubdesc()));
	   	}		
	   	if (costCubDto.getParcostCatecode() != null && !"".equals(costCubDto.getParcostCatecode())) {
	   		jpql.append(" and _costSub.parcostCatecode = ?");
	   		conditionVals.add(costCubDto.getParcostCatecode());
	   	}		
	   	if (costCubDto.getDccCode() != null && !"".equals(costCubDto.getDccCode())) {
	   		jpql.append(" and _costSub.dccCode = ?");
	   		conditionVals.add(costCubDto.getDccCode());
	   	}		
	   	if (costCubDto.getIsDetail() != null && !"".equals(costCubDto.getIsDetail())) {
	   		jpql.append(" and _costSub.isDetail = ?");
	   		conditionVals.add(costCubDto.getIsDetail());
	   	}
	   	//jpql.append(" order by to_number(_costSub.costSubcode) ");
        @SuppressWarnings("unchecked")
		Page<CostSub> pages = getQueryChannelService()
		   .createJpqlQuery(jpql.toString())
		   .setParameters(conditionVals)
		   .setPage(currentPage, pageSize)
		   .pagedList();
		   
        return new Page<CostSubDTO>(pages.getStart(), pages.getResultCount(),pageSize, CostSubAssembler.toDTOs(pages.getData()));
	}

	@Override
	public Boolean isCostSubCodeExist(String costSubcode) {
		String sql = "select count(1) from drgs.t_costsub t where t.cost_sub_code = :costSubcode " ;
		Map<String,Object> condition = new HashMap<>() ;
		condition.put("costSubcode", costSubcode) ;
		int count = application.queryForInt(sql,condition) ;
		if(count>0) {
			return true ;
		}
		return false;
	}
	
}
